package com.lvhaosb.newpneumonia.service.impl;

import com.lvhaosb.newpneumonia.POJO.vo.ForeignEpidemicInfo;
import com.lvhaosb.newpneumonia.POJO.po.Country;
import com.lvhaosb.newpneumonia.POJO.po.ForeignEpidemic;
import com.lvhaosb.newpneumonia.mapper.CountryMapper;
import com.lvhaosb.newpneumonia.mapper.ForeignEpidemicMapper;
import com.lvhaosb.newpneumonia.service.ForeignEpidemicService;
import com.lvhaosb.newpneumonia.tools.ClassTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 国外新冠疫情 服务实现类
 *
 * @author lvhao
 * @since 2020-06-21
 */
@Service
public class ForeignEpidemicServiceImpl implements ForeignEpidemicService {
    @Autowired
    private CountryMapper countryMapper;
    @Autowired
    private ForeignEpidemicMapper foreignEpidemicMapper;

    @Override
    public List<ForeignEpidemicInfo> queryForeignEpidemicInfo() {
        //查询最新的国外各国疫情信息
        //1. 获取当前有数据的国家
        List<Country> countryList = countryMapper.selectAll();
        //2. 生成返回数据列表
        List<ForeignEpidemicInfo> list = new ArrayList<>();
        for(Country country: countryList){
            //3. 查询国家疫情信息并包装数据加入列表
            ForeignEpidemic foreignEpidemic = foreignEpidemicMapper.selectByTimeNewest(country.getId()).get(0);
            //赋值
            ForeignEpidemicInfo foreignEpidemicInfo = new ForeignEpidemicInfo();
            foreignEpidemicInfo.setCountry(country.getName());//国家
            foreignEpidemicInfo.setCure(foreignEpidemic.getCure());//治愈
            foreignEpidemicInfo.setCumulative(foreignEpidemic.getCumulative());//累计
            foreignEpidemicInfo.setDied(foreignEpidemic.getDied());//死亡
            foreignEpidemicInfo.setExisting(foreignEpidemic.getExisting());//现有
            foreignEpidemicInfo.setNewlyAdded(foreignEpidemic.getNewlyAdded());//新增
            foreignEpidemicInfo.setTime(foreignEpidemic.getTime().toString());//时间
            //加入列表
            list.add(foreignEpidemicInfo);
        }
        return list;
    }

    @Override
    @Transactional
    public Boolean save(ForeignEpidemicInfo foreignEpidemicInfo) {
        // 1. 判断国家列表是否有该国家，如果没有则添加该国家到数据库
        if(countryMapper.selectByName(foreignEpidemicInfo.getCountry()).isEmpty()){
            Country country = new Country();
            country.setName(foreignEpidemicInfo.getCountry());
            int insert = countryMapper.insert(country);
            if(insert>0){
                System.out.println(""+country.getName()+"国家已添加");
            }
        }
        // 2. 添加该国家的疫情信息
        Country country = countryMapper.selectByName(foreignEpidemicInfo.getCountry()).get(0);
        ForeignEpidemic foreignEpidemic = new ForeignEpidemic();
        ClassTool classTool = new ClassTool();
        try {
            classTool.classAssignment(foreignEpidemicInfo, foreignEpidemic);
            foreignEpidemic.setCountryId(country.getId());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        foreignEpidemic.setTime(LocalDateTime.now());//时间
        int insert = foreignEpidemicMapper.insert(foreignEpidemic);
        if(insert>0){
            return true;
        }
        return false;
    }
}
